﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BLL;
using DevComponents.DotNetBar;

namespace GUI
{
    public partial class frmLop : DevComponents.DotNetBar.Office2007Form
    {
        private BOKy _ky;
        private BOLop _lop;
        private DataSet _ds;
        private BOLog _log;
        public frmLop()
        {
            InitializeComponent();
            descSizeHeight();
        }

        #region "Tăng giảm kích thước"
        public void incSizeHeight()
        {
            int y = Size.Height;
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    Thread.Sleep(5);
                    Size = new Size(Size.Width, y + i * 7);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            CenterToScreen();
        }

        public void descSizeHeight()
        {
            int y = Size.Height;
            for (int i = 0; i < 20; i++)
            {
                try
                {
                    Thread.Sleep(5);
                    this.Size = new Size(Size.Width, y - i * 7);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            CenterToScreen();
        }
        #endregion

        private void frmLop_Load(object sender, EventArgs e)
        {
            _lop = new BOLop();
            _ds = _lop.Binding();
            dgvLop.DataSource = _ds.Tables[0];
            dgvLop.Columns[0].Width = 163;
            dgvLop.Columns[1].Width = 100;
            dgvLop.Columns[0].HeaderText = "Tên Lớp";
            dgvLop.Columns[1].HeaderText = "Kỳ";

            _ky = new BOKy();
            _ds = _ky.Binding();
            cbxKy.DataSource = _ds.Tables[0];
            cbxKy.DisplayMember = "TenKy";
            cbxKy.ValueMember = "Ky";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            incSizeHeight();
            txtTenLop.Enabled = true;
            txtTenLop.Focus();
            btnThem.Enabled = false;
            btnThoat.Enabled = false;
            btnUpdate.Enabled = false;
            btnOK.Enabled = false;
            btnSua.Enabled = false;
            txtTenLop.Text = "";
            cbxKy.SelectedIndex = 0;
            dgvLop.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvLop.CurrentRow == null)
            {
                MessageBoxEx.Show("Bạn phải chọn 1 lớp !!!");
            }
            else
            {
                incSizeHeight();
                btnThem.Enabled = false;
                btnThoat.Enabled = false;
                btnSua.Enabled = false;
                txtTenLop.Enabled = false;
                string tenMonHoc = dgvLop.CurrentRow.Cells[0].Value.ToString();
                int ky = Convert.ToInt32(dgvLop.CurrentRow.Cells[1].Value.ToString());
                txtTenLop.Text = tenMonHoc;
                cbxKy.SelectedValue = ky;
                btnOK.Hide();
                btnUpdate.Enabled = true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string tenLop = txtTenLop.Text;
            int ky = (int)cbxKy.SelectedValue;
            _lop = new BOLop();
            int result = _lop.AddLop(tenLop, ky);
            MessageBoxEx.Show(result > 0 ? "Thêm mới thành công !!!" : "Thêm mới thất bại !!!");
            descSizeHeight();
            frmLop_Load(sender, e);
            _log = new BOLog();
            _log.AddLog(Session.member.TenThanhVien, " thêm lớp " + tenLop, DateTime.Now, "");
            btnThem.Enabled = true;
            btnThoat.Enabled = true;
            btnSua.Enabled = true;
            dgvLop.Enabled = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string tenLop = txtTenLop.Text;
            int ky = (int) cbxKy.SelectedValue;
            _lop = new BOLop();
            int result = _lop.Update(tenLop, ky);
            MessageBoxEx.Show(result > 0 ? "Cập nhật thành công !!!" : "Cập nhật thất bại !!!");
            descSizeHeight();
            frmLop_Load(sender, e);
            _log = new BOLog();
            _log.AddLog(Session.member.TenThanhVien, " sửa thông tin lớp " + tenLop, DateTime.Now, "");
            btnThem.Enabled = true;
            btnThoat.Enabled = true;
            btnSua.Enabled = true;
            dgvLop.Enabled = true;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            dgvLop.Enabled = true;
            txtTenLop.Text = "";
            txtTenLop.Enabled = true;
            cbxKy.SelectedIndex = 0;
            descSizeHeight();
            btnThem.Enabled = true;
            btnThoat.Enabled = true;
            btnSua.Enabled = true;
            btnOK.Show();
            btnUpdate.Enabled = true;
        }

        private void txtTenLop_TextChanged(object sender, EventArgs e)
        {
            if (txtTenLop.Text.Equals(""))
                btnOK.Enabled = false;
            else
                btnOK.Enabled = true;
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text.Equals(""))
            {
                frmLop_Load(sender, e);
            }
            else
            {
                if (cbxSearch.SelectedIndex == 0)
                {
                    var tenLop = txtSearch.Text;
                    var ds = _lop.SearchByTenLop(tenLop);
                    dgvLop.DataSource = ds.Tables[0];
                }
                if (cbxSearch.SelectedIndex == 1)
                {
                    int ky = Convert.ToInt32(txtSearch.Text);
                    var ds = _lop.SearchByKy(ky);
                    dgvLop.DataSource = ds.Tables[0];
                }
            }
        }

        private void frmLop_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr;
            MessageBoxEx.EnableGlass = false;
            dr = MessageBoxEx.Show("Bạn có thật sự muốn thoát ?", "Thông báo", MessageBoxButtons.YesNo,
                                   MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                e.Cancel = true;
            }
        }
    }
}